<?php

namespace App\Http\Controllers\Backend;

use App\Models\Card;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Backend\CardRequests;

class CardsController extends Controller
{
    public function index()
    {
        $cards = Card::where('status', true)->get();
        return view('backend.cards.index', compact('cards'));
    }

    public function create()
    {
        return view('backend.cards.create');
    }


    public function store(CardRequests $request)
    {
        if(Card::create($request->all())){
            session()->flash('message', '添加成功');
            return redirect(route('admin.cards.index'));
        }

    }


    public function edit(Request $request, Card $card)
    {
        return view('backend.cards.edit', compact('card'));
    }

    public function update(Request $request, Card $card)
    {
        $card->update($request->all());
        session()->flash('message', '更新成功');
        return redirect(route('admin.cards.index'));
    }


    public function destroy(Card $card)
    {
        $card->user()->detach();
        $card->delete();
        session()->flash('message', '删除成功');
        return redirect(route('admin.cards.index'));

    }

    public function muilt()
    {
        factory(Card::class, 50)->create();
        session()->flash('message', '添加成功');
        return redirect(route('admin.cards.index'));
    }
}
